<?php


function excel_list_komponen($pr_yearly_income_id){
	$pr_yearly_incomes=my_get_data_by_id('pr_yearly_income','pr_yearly_income_id',(int) $pr_yearly_income_id) ;
	 
	$header = array(  
		'Komponen'=>array('style'=>'border-bottom:2px solid;width:50%'), 
		'Rupiah'=>array('style'=>'text-align:right;border-bottom:2px solid;width:50%'),   
	);
	$query = "
			SELECT * FROM pr_yearly_income_komponen  
			WHERE pr_yearly_income_id={$pr_yearly_income_id} AND tipe ='Detil'
			ORDER BY komponen_id
		";	
 
	$result = my_query($query );
	$row = array();  
	while($ey = my_fetch_array($result)){
		$nilai_rupiah = get_nominal_by_komponen_id($ey['komponen_id'] ,$pr_yearly_income_id);  
		$nilai_rupiah = round($nilai_rupiah );
		$row[] = array( 
			'komponen'=> $ey['label']  , 
			'total_komponen'=>  ($nilai_rupiah < 0 ) ? (-1) *$nilai_rupiah : $nilai_rupiah  ,  
		);	
	}
 
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,2 ,false ); 	
}
 
function list_komponen(  $pr_komponen_id){
	$pr_yearly_income_id = get_pr_yearly_income_id( );
	$pr_yearly_incomes=my_get_data_by_id('pr_yearly_income','pr_yearly_income_id',(int) $pr_yearly_income_id) ;
	 
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Komponen'=>array('style'=>'border-bottom:2px solid;width:50%'),  
		//'Dasar Nilai'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		//'Pajak'=>array('style'=>'border-bottom:2px solid;width:10%'),  
		'Rupiah'=>array('style'=>'text-align:right;border-bottom:2px solid;width:30%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:5%'), 
	);
	$query = "
			SELECT * FROM pr_yearly_income_komponen a 
			WHERE pr_yearly_income_id={$pr_yearly_income_id} AND tipe ='Detil'  
			ORDER BY komponen_id
		";	
 ;		
	$result = my_query($query );
	$row = array(); 
	$i=0;
	while($ey = my_fetch_array($result)){
	 
			$detailproperty = array(
				'href'=>'index.php?com='.$_GET['com'].'&task=detail_komponen&pr_yearly_income_id='.$pr_yearly_income_id.'&komponen_id='.$ey['komponen_id'],
				'title'=>'Open detail data'
			);
			$detail_button = button_icon( 'b_bookmark.png' , $detailproperty  );			
			$nilai_rupiah = get_nominal_by_komponen_id($ey['komponen_id'] , $pr_yearly_income_id );
			$rupiah=rp_format($nilai_rupiah) ;
			$i++;
			$row[] = array(
				'#'=>position_text_align ($i, 'center'), 
				'komponen'=> $ey['label']  ,
			//	'formula'=> $formula  , 
			//	'pajak'=> $ey['kena_pajak']  , 
				'total_komponen'=>position_text_align( $rupiah , 'right' ), 
				' v'=>position_text_align($detail_button,'right'),
			);			
		 	
	}
	$tahuns = array();
	for($thn = date('Y'); $thn >= 2010; $thn--){
		$tahuns[$thn]=$thn;
	}
	$tahun = array(
		'name'=>'tahun',
		'value'=>isset($_GET['tahun']) ? $_GET['tahun']: '',
	);
	
	$query = "SELECT * FROM yearly_income";
	$res = my_query($query);
	$yinc = array();
	while($ro = my_fetch_array($res)){
		$yinc[$ro['yearly_income_id']] = $ro['label'];
	}
	$nm = array(
		'name'=>'yearly_income_id',
		'value'=>isset($_GET['yearly_income_id']) ? $_GET['yearly_income_id'] : 0 ,
	);
	$view  = form_header( "..." , "..."  );
	$view .=form_field_display( '<span class="label_form">'.  form_dropdown($tahun , $tahuns).'</span>'   , "<b>Tahun</b>"    ) ;
	$view .=form_field_display( '<span class="label_form">'.  form_dropdown($nm , $yinc).'</span>'   , "<b>Type of income</b>"    ) ;
//	$view .=form_field_display( '<span class="label_form">'.  date( "m-Y" , strtotime($pr_yearly_incomes['tanggal'])) .'</span>'   , "<b>Periode Bayar</b>"    ) ;
	//$periodes = my_get_data_by_id('pr_periode','periode_id',$pr_yearly_incomes['periode_id']);
	$task = array(
		'value'=>$_GET['task'],
		'name'=>'task', 
		'type'=>'hidden'
	);
	$com = array(
		'value'=>$_GET['com'],
		'name'=>'com', 
		'type'=>'hidden'
	);
	$submit = array(
		'value'=>'Submit',
		'name'=>'Submit',
		'class'=>'main_button',
		'type'=>'Submit'
	);
	$view .=form_field_display( form_dynamic($submit).form_dynamic($com).form_dynamic($task)    , "&nbsp;"    ) ;
	
	$view  .= form_footer(  ); 
	$view = str_replace('method="post"','method="GET"',$view);
	$menuontop = array(  
		'Excel' => array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&pr_yearly_income_id='.(int) $pr_yearly_income_id.'&task=excel_list_komponen\''), 
	//	'Unproses' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_yearly_income&task=unproses&pr_yearly_income_id='.$pr_yearly_income_id.'\''),
	//	'List Karyawan' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_yearly_income&task=list_karyawan&pr_yearly_income_id='.$pr_yearly_income_id.'\''),
	);	
	 
	$printmenu = additional_menu_on_list( $menuontop ,'' ); 
	$datas = table_rows($row);  
	return $view.$printmenu.table_builder($header , $datas , 6, false ,$paging);
}
 
function get_pr_yearly_income_id(){
	$yearly_income_id = isset($_GET['yearly_income_id']) ? (int) $_GET['yearly_income_id'] : 0;
	$tahun = isset($_GET['tahun']) ?   $_GET['tahun'] : 0;
	$query = "SELECT pr_yearly_income_id FROM pr_yearly_income WHERE yearly_income_id = {$yearly_income_id} AND tahun='{$tahun}' ";
	$res = my_query($query);
	$row =my_fetch_array($res);
	return (int) $row['pr_yearly_income_id'];
}
 
 

function get_nominal_by_komponen_id($komponen_id, $pr_yearly_income_id){
	$query = "SELECT SUM(nominal) AS total FROM pr_yearly_income_karyawan 
	WHERE komponen_id = {$komponen_id} AND pr_yearly_income_id = {$pr_yearly_income_id} ";
	 
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['total'];
}

function excel_list_komponen_detail($komponen_id , $pr_yearly_income_id){
$header = array( 
		'NIK'=>array('style'=>'border-bottom:2px solid;width:8%'),   
		'Nama karyawan'=>array('style'=>'border-bottom:2px solid;width:27%'),   
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:60%'),   
		'Rupiah'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%') 
	);
	$query = "
			SELECT a.karyawan_id,a.nominal FROM pr_yearly_income_karyawan a
			INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id 
			WHERE a.komponen_id={$komponen_id}  AND pr_yearly_income_id ={$pr_yearly_income_id} AND a.nominal <> 0
			
			
		AND b.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  ) 
		AND b.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
		ORDER BY b.nik ASC
		";	

	$result = my_query($query);
	while($ey = my_fetch_array($result)){ 
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$jabatan = loaddata_jabatan($karyawan['jabatan_id']);
		$row[] = array( 
				'nik'=> "'".$karyawan['nik']  ,  
				'nama'=> $karyawan['nama_gelar']  ,  
				'jabatan'=> $jabatan['nama_jabatan']  , 
				'd'=> round( ($ey['nominal'] < 0) ? (-1) *$ey['nominal'] : $ey['nominal'] ) ,  
				 
			);	
	}
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,2 ,false ); 	
}

function list_komponen_detail($komponen_id , $pr_yearly_income_id){
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:8%'),   
		'Nama karyawan'=>array('style'=>'border-bottom:2px solid;width:27%'),   
		'Jabatan'=>array('style'=>'border-bottom:2px solid;width:40%'),   
		'Rupiah'=>array('style'=>'text-align:right;border-bottom:2px solid;width:20%') 
	);
	$query = "
			SELECT a.karyawan_id,a.nominal FROM pr_yearly_income_karyawan a
			INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id 
			WHERE a.komponen_id={$komponen_id}  AND pr_yearly_income_id ={$pr_yearly_income_id} AND a.nominal <> 0
			
			
		AND b.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  ) 
		AND b.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			ORDER BY b.nik ASC
		";	

	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&pr_yearly_income_id={$_GET['pr_yearly_income_id']}&komponen_id={$_GET['komponen_id']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
				
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
		$karyawan = loaddata_karyawan($ey['karyawan_id']);
		$jabatan = loaddata_jabatan($karyawan['jabatan_id']);
		$row[] = array(
				'#'=>position_text_align ($i, 'center'), 
				'nik'=> $karyawan['nik']  ,  
				'nama'=> $karyawan['nama_gelar']  ,  
				'jabatan'=> $jabatan['nama_jabatan']  , 
				'd'=>position_text_align( rp_format($ey['nominal']) , 'right' ), 
				 
			);	
	}
	$pr_yearly_income = my_get_data_by_id('pr_yearly_income','pr_yearly_income_id', (int) $pr_yearly_income_id); 
	$yearly = my_get_data_by_id('yearly_income','yearly_income_id', (int) $pr_yearly_income['yearly_income_id']); 
	$komponen = my_get_data_by_id('pr_yearly_income_komponen','komponen_id', (int) $komponen_id); 
	$view  = form_header( "..." , "..."  );
	$view .=form_field_display( '<span class="label_form">'. $yearly['label'].' '. $pr_yearly_income['tahun'] .'</span>'   , "<b>Yearly Income</b>"    ) ; 
	$view .=form_field_display( '<span class="label_form">'.  $komponen['label'].'</span>'   , "<b>Item Komponen</b>"    ) ;
 
	$view  .= form_footer(  ); 
	$menuontop = array(  
		'Cetak' => array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&komponen_id='.$komponen_id.'&pr_yearly_income_id='.(int) $pr_yearly_income_id.'&task=cetak_komponen_detail\''), 
		'Excel' => array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&komponen_id='.$komponen_id.'&pr_yearly_income_id='.(int) $pr_yearly_income_id.'&task=excel_list_komponen_detail\''), 
	);	
	 
	$printmenu = additional_menu_on_list( $menuontop ,'' ); 
	$datas = table_rows($row);  
	
	$paging = $kgPagerOBJ ->showPaging();
	return $view.$printmenu.table_builder($header , $datas , 6, false ,$paging);
}


